package com.situ.b2c.product.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.situ.b2c.pojo.Product;
import com.situ.b2c.product.service.ProductService;



@WebServlet("/ProductSearchServlet")
public class ProductSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(ProductSearchServlet.class);
    
    private ProductService productService;
	

	public ProductSearchServlet() {
		ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
		productService = context.getBean(ProductService.class);
	
	}
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		
		Product searchProduct = new Product();
		
		String marketPrice = request.getParameter("marketPrice");
		if (marketPrice != null && !"".equals(marketPrice)) {
			searchProduct.setMarketPrice(Double.parseDouble(marketPrice));
		}
		String prodQuantity = request.getParameter("prodQuantity");
		if (prodQuantity != null && !"".equals(prodQuantity)) {
			searchProduct.setProdQuantity(Integer.parseInt(prodQuantity));
		}
		String isSale = request.getParameter("isSale");
		if (isSale != null && !"".equals(isSale)) {
			searchProduct.setIsSale(Integer.parseInt(isSale));
		}
		String prodName = request.getParameter("prodName");
		
		searchProduct.setProdName(prodName);
		
		List<Product> prodList = productService.selectBySearch(searchProduct);
		LOG.info(prodList);
		request.setAttribute("product", prodList);
		request.getRequestDispatcher("B2Choutai/product_show.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
